/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package eyrolles;

import ejb.EyrollesRemote;
import exception.*;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 *
 * @author Nairo
 */
public class EyrollesModeleEJB {

    EyrollesRemote eyrolles = lookupEyrollesRemote();
    private Properties properties;
    private DataSource datasource;

    public void ajouterEmploye(Employe employe, String password)
            throws ServiceException, DataAccessException {
        try {
            eyrolles.ajouterEmploye(employe, password);
        } catch (EmployeServiceException ese) {
            throw new ServiceException(ese.getMessage());
        } catch (EmployeDataException ede){
            throw new DataAccessException(ede.getMessage());
        }
    }

    public void supprimerEmploye(String username) throws ServiceException, DataAccessException {
        try {
            eyrolles.supprimerEmploye(username);
        } catch (EmployeServiceException ese) {
            throw new ServiceException(ese.getMessage());
        } catch (EmployeDataException ede){
            throw new DataAccessException(ede.getMessage());
        }
    }

    public void modifierEmploye(Employe emp, String password) throws ServiceException, DataAccessException {
        try {
            eyrolles.modifierEmploye(emp, password);
        } catch (EmployeServiceException ese) {
            throw new ServiceException(ese.getMessage());
        } catch (EmployeDataException ede){
            throw new DataAccessException(ede.getMessage());
        }
    }

    public ArrayList<Employe> recupererEmployes() throws ServiceException, DataAccessException {
        try {
            return eyrolles.recupererEmployes();
        } catch (EmployeServiceException ese) {
            throw new ServiceException(ese.getMessage());
        } catch (EmployeDataException ede){
            throw new DataAccessException(ede.getMessage());
        }
    }

    public Employe recupererEmploye(String username) throws ServiceException, DataAccessException, UserNotFoundException {
        try {
            return eyrolles.recupererEmploye(username);
        } catch (EmployeServiceException ese) {
            throw new ServiceException(ese.getMessage());
        } catch (EmployeDataException ede){
            throw new DataAccessException(ede.getMessage());
        }
    }

    public String recupererPassword(String username) throws ServiceException, DataAccessException {
        try {
            return eyrolles.recupererPassword(username);
        } catch (EmployeServiceException ese) {
            throw new ServiceException(ese.getMessage());
        } catch (EmployeDataException ede){
            throw new DataAccessException(ede.getMessage());
        }
    }

    public String connecterEmploye(String username, String password) throws ServiceException, DataAccessException, UserLoginException {
        try {
            return eyrolles.connecterEmploye(username, password);
        } catch (EmployeServiceException ese) {
            throw new ServiceException(ese.getMessage());
        } catch (EmployeDataException ede){
            throw new DataAccessException(ede.getMessage());
        }
    }

    private EyrollesRemote lookupEyrollesRemote() {
        try {
            Context c = new InitialContext();
            return (EyrollesRemote) c.lookup("java:global/EJBEyrolles/Eyrolles!ejb.EyrollesRemote");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
